#!/usr/bin/python3

import re

from PIL import Image

from PIL import ImageFont

from PIL import ImageDraw

import sys

pattern = r"^#"

# check the files

if len(sys.argv)<2 :
    print ("Usage: %s filename.txt" % sys.argv[0])
    exit(-1)



# 设置文本文件的路径

txt_file_path = sys.argv[1]

# 设置输出图片的路径和尺寸

jpg_file_path = sys.argv[1] + ".jpg"

##image_width, image_height = 800, 18600

# 创建一个新的白色图片

# image = Image.new('RGB', (image_width, image_height), color = 'black')

# 设置字体

font = ImageFont.truetype('Hiragino Sans GB.ttc', 20)

# 在图片上绘制文本

#draw = ImageDraw.Draw(image)

with open(txt_file_path, 'r') as file:

    lines = file.readlines()
    num_lines = len(lines)
    counter=1

# 创建一个新的白色图片

    image_width = 800

    image_height = num_lines * 30

    image = Image.new('RGB', (image_width, image_height), color = 'black')
    draw = ImageDraw.Draw(image)

    for line in lines:
        match = re.match(pattern,line)
        if match:
            draw.text((10, 30*counter), line, font=font, fill=(255, 0, 0))
        else:
            draw.text((10, 30*counter), line, font=font, fill=(255, 255, 255))
        counter = counter+1

# 保存图片

image.save(jpg_file_path)

